// PatchSample.cpp: implementation of the CPatchSample class.
//
//////////////////////////////////////////////////////////////////////
#include "PatchSample.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPatchSample::CPatchSample()
{
	m_pimgsrc = NULL;
	m_pimgitg = NULL;

	m_bNew = false;
}

CPatchSample::~CPatchSample()
{
	Clear();
}

void CPatchSample::Clear()
{
	ClearImage();

	m_vfMean.resize(0);
	m_vfSTD.resize(0);
}

void CPatchSample::ClearImage()
{
	if (m_bNew)
	{
		if (m_pimgsrc != NULL) delete m_pimgsrc;
		if (m_pimgitg != NULL) delete m_pimgitg;
		m_bNew = false;
	}
	m_pimgsrc = NULL;
	m_pimgitg = NULL;
}
 
/* Jiadi Yang
BOOL CPatchSample::LoadImage(const char *strFileName)
{
	ClearImage();

	Raster<uchar> image_org;

	if (LoadImageFile2Raster(image_org, strFileName) == 0)
		return FALSE;

	m_rect.left = 0;	m_rect.right = image_org.Width()-1;
	m_rect.top	= 0;	m_rect.bottom= image_org.Height()-1;

	h = m_rect.Height();
	w = m_rect.Width();

	m_pimgitg = new McItgImage;
	m_pimgitg->Prepare(*m_pimgsrc);
	s = m_pimgitg->CalcuSTD(m_rect);

	m_bNew = TRUE;

	return TRUE;
}
*/

void CPatchSample::SetImages(Raster<uchar> *psrc, McItgImage *pitg)
{
	ClearImage();

	m_pimgsrc = psrc;
	m_pimgitg = pitg;

	s = m_pimgitg->CalcuSTD(m_rect);

	m_bNew = false;
}


//
// input: vFeaID
// output:vfFeatures
//
int CPatchSample::GetFeatures(vfloat &vfFeatures, vfeaid &vFeaID)
{
	CFeaID			*pfid;
	CHarrFeature	*phr;

	vfFeatures.resize(0);
	for (int i = 0; i < vFeaID.size(); i ++)
	{
		pfid = &(vFeaID[i]);
		if (pfid->strFeaName[0] == 'H' && pfid->strFeaName[1] == 'R')
		{
			phr = &(m_pHarrs->m_vHarr[pfid->nFeaIdx]);
			vfFeatures.push_back(GetHarrFeature(*phr));
		}
	}
	return vfFeatures.size();
}

//
// input: vFeaID
// output: feature
//
double CPatchSample::GetHarrFeature(CHarrFeature &hr)
{
	int hw = (hr.w * (w-1) + 15) / 31;
	int hh = (hr.h * (h-1) + 15) / 31;
	int hx = (hr.x * (w-1) + 15) / 31 + m_rect.left;
	int hy = (hr.y * (h-1) + 15) / 31 + m_rect.top;
	return m_pimgitg->HarrFeature(hr.type, hw, hh, hx, hy) / s;
}

